package Test;
import java.io.*;
import java.lang.String;

public class StarDictLoader {
	
	static final int IdxArraySize = 40000;
	static final int CatalogSize = 800000;
	static final int ExplainSize = 100000;
	
	static final String DicFileName = "oxford.dic";
	static final String IdxFileName = "oxford.idx";
	
	private RandomAccessFile dicFile;
	
	private int[] idxArray = new int[IdxArraySize];
	private byte[] catalog = new byte[CatalogSize];
	private byte[] dicData = new byte[ExplainSize];

	protected int mCount = 0;
	protected int mIndex = 0;
	
	protected void Import() {
		int idxFileSize = 0;
		try {
			dicFile = new RandomAccessFile(DicFileName, "r");
			DataInputStream idxData = new DataInputStream(new FileInputStream(IdxFileName));
			idxFileSize = idxData.read(catalog);
			idxData.close();
			assert(idxFileSize < CatalogSize);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		int offset = 0, curpos = 0;
		while (curpos < idxFileSize)
		{
			if (catalog[curpos] == '\0') {
				idxArray[mCount++] = offset;
				offset = curpos + 9;
				curpos = offset;
			} else {
				++curpos;
			}
		}
	}
	
	protected void Release() {
		try {
			dicFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	protected String GetTrans() {
		return GetTrans(mIndex);
	}
	
	protected String GetTrans(int key) {
		int offset = idxArray[key];
		int length = 0;
		while(catalog[offset + length] != '\0')
			++length;
		++length;
		
		int dicOffset = ReadInt(offset + length);
		int dicLen = ReadInt(offset + length + 4);
		assert(dicLen < ExplainSize);
		
		String trans = "Trans Not Found";
		try {
			dicFile.seek(dicOffset);
			dicFile.read(dicData, 0, dicLen);
			trans = new String(dicData, 0, dicLen, "GBK");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return trans;
	}
	
	private String GetWord(int key) {
		int offset = idxArray[key];
		int length = 0;
		while(catalog[offset + length] != '\0')
			++length;
		
		return new String(catalog, idxArray[key], length);
	}
	
	private int ReadInt(int offset)
	{
		int mask=0xff;
        int temp=0;
        int res=0;
        for(int i=0;i<4;i++){
        	res <<= 8;
        	temp = catalog[offset+i] & mask;
        	res |= temp;
        }
        return res;
	}
	/*
	public static void main(String args[]){
		StarDictLoader test = new StarDictLoader();
		DictInfo dict = new DictInfo();
		test.Import();
		for(int i=0; i<test.mCount; ++i){
			WordInfo word = new WordInfo();
			dict.words.add(word);
			word.items[0] = test.GetWord(i);
			if(word.items[0].equals("zone")){
				System.out.println("fuck you here");
			}
			String trans = test.GetTrans(i) + "\n";
			CharSequence cs = (CharSequence)trans;
			int j = trans.indexOf("\n");
			while(j != -1){
				if(j != 0){
					WordInfo.TypeInfo type = word.new TypeInfo();
					word.infos.add(type);
					type.items[0] = trans.substring(0, j);
				}
				trans = trans.substring(j + 1);
				j = trans.indexOf("\n");
			}
		}
		try{
			FileWriter writer = new FileWriter("Dict.txt");
			dict.Export(writer);
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
	*/
}
